home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware in MacFormat / brailler0.5b / brlr ƒ / Shell ƒ / text twiddling.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-20  |  7.9 KB  |  379 lines  |  [TEXT/MMCC]

  1. #include "text twiddling.h"
  2. #include "window layer.h"
  3.  
  4. static    asm void MyDrawHook(void);
  5.  
  6. static    ProcPtr        gOldDrawHook;
  7.  
  8. Boolean AnyTextInScrapQQ(void)
  9. {
  10.     long            dummy;
  11.     
  12.     LoadScrap();
  13.     return (GetScrap(0L, 'TEXT', &dummy)!=noTypeErr);
  14. }
  15.  
  16. void SetTheText(WindowPtr theWindow, Ptr data, long count)
  17. {
  18.     TEHandle        hTE;
  19.     ControlHandle    vScrollBar;
  20.     
  21.     hTE=GetWindowTE(theWindow);
  22.     if (hTE==0L)
  23.         return;
  24.     
  25.     vScrollBar=GetWindowVScrollBar(theWindow);
  26.     
  27.     TESetText(data, count, hTE);
  28.     TESetSelect(0, 0, hTE);
  29.     if (vScrollBar!=0L)
  30.         AdjustVScrollBar(vScrollBar, hTE);
  31. }
  32.  
  33. Boolean AnyTextQQ(WindowPtr theWindow)
  34. {
  35.     TEHandle        hTE;
  36.     
  37.     hTE=GetWindowTE(theWindow);
  38.     if (hTE==0L)
  39.         return FALSE;
  40.     else
  41.         return ((**hTE).teLength!=0);
  42. }
  43.  
  44. Boolean AnyHighlightedQQ(WindowPtr theWindow)
  45. {
  46.     TEHandle        hTE;
  47.     
  48.     hTE=GetWindowTE(theWindow);
  49.     if (hTE==0L)
  50.         return FALSE;
  51.     else
  52.         return ((**hTE).selStart!=(**hTE).selEnd);
  53. }
  54.  
  55. short SelectionStart(WindowPtr theWindow)
  56. {
  57.     TEHandle        hTE;
  58.     
  59.     hTE=GetWindowTE(theWindow);
  60.     if (hTE==0L)
  61.         return -1;
  62.     
  63.     return (**hTE).selStart;
  64. }
  65.  
  66. short SelectionEnd(WindowPtr theWindow)
  67. {
  68.     TEHandle        hTE;
  69.     
  70.     hTE=GetWindowTE(theWindow);
  71.     if (hTE==0L)
  72.         return -1;
  73.     
  74.     return (**hTE).selEnd;
  75. }
  76.  
  77. Boolean InsertBeforeStart(WindowPtr theWindow, Str255 theStr)
  78. {
  79.     TEHandle        hTE;
  80.     unsigned long    len;
  81.     unsigned long    oldLen;
  82.     
  83.     len=theStr[0];
  84.     hTE=GetWindowTE(theWindow);
  85.     oldLen=(**hTE).teLength;
  86.     if (oldLen+len>32767)
  87.         return FALSE;
  88.     
  89.     TEInsert(&theStr[1], len, hTE);
  90.     return TRUE;
  91. }
  92.  
  93. Boolean InsertAfterEnd(WindowPtr theWindow, Str255 theStr)
  94. {
  95.     TEHandle        hTE;
  96.     unsigned long    len;
  97.     unsigned long    oldLen;
  98.     unsigned long    oldSelStart;
  99.     unsigned long    oldSelEnd;
  100.     
  101.     len=theStr[0];
  102.     hTE=GetWindowTE(theWindow);
  103.     oldLen=(**hTE).teLength;
  104.     if (oldLen+len>32767)
  105.         return FALSE;
  106.     
  107.     oldSelStart=(**hTE).selStart;
  108.     oldSelEnd=(**hTE).selEnd;
  109.     TESetSelect(oldSelEnd, oldSelEnd, hTE);
  110.     TEInsert(&theStr[1], len, hTE);
  111.     TESetSelect(oldSelStart, oldSelEnd, hTE);
  112.     return TRUE;
  113. }
  114.  
  115. short TotalNumberOfLines(TEHandle hTE)
  116. {
  117.     short            numLines;
  118.     
  119.     numLines=(**hTE).nLines;
  120.     if (*((unsigned char*)((long)(*((**hTE).hText))+(**hTE).teLength-1))==0x0d)
  121.         numLines++;
  122.     
  123.     return numLines;
  124. }
  125.  
  126. pascal void ScrollActionProc(ControlHandle theHandle, short partCode)
  127. {
  128.     short            scrollDistance;
  129.     TEHandle        hTE;
  130.     WindowPtr        theWindow;
  131.     
  132.     theWindow=(**theHandle).contrlOwner;
  133.     if (theWindow==0L)
  134.         return;
  135.     
  136.     hTE=GetWindowTE(theWindow);
  137.     if (hTE==0L)
  138.         return;
  139.     
  140.     switch (partCode)
  141.     {
  142.         case inUpButton:
  143.         case inDownButton:
  144.             scrollDistance=(**hTE).lineHeight;
  145.             break;
  146.         case inPageUp:
  147.         case inPageDown:
  148.             scrollDistance=(**hTE).viewRect.bottom-(**hTE).viewRect.top-(**hTE).lineHeight;
  149.             break;
  150.         default:
  151.             scrollDistance=0;
  152.             break;
  153.     }
  154.     
  155.     if ((partCode==inDownButton) || (partCode==inPageDown))
  156.         scrollDistance=-scrollDistance;
  157.     
  158.     MyMoveScrollBox(theHandle, scrollDistance/(**hTE).lineHeight);
  159.     
  160.     if (scrollDistance!=0)
  161.     {
  162.         TEPinScroll(0, scrollDistance, hTE);
  163.     }
  164. }
  165.  
  166. void MyMoveScrollBox(ControlHandle theControl, short scrollDistance)
  167. {
  168.     short            oldSetting, setting, max;
  169.     
  170.     oldSetting=GetControlValue(theControl);
  171.     max=GetControlMaximum(theControl);
  172.     setting=oldSetting-scrollDistance;
  173.     if (setting<0)
  174.         setting=0;
  175.     else if (setting>max)
  176.         setting=max;
  177.     
  178.     SetControlValue(theControl, setting);
  179. }
  180.  
  181. void AdjustVScrollBar(ControlHandle theControl, TEHandle hTE)
  182. {
  183.     short            oldValue, oldMax;
  184.     short            numLines, max, value;
  185.     
  186.     oldMax=GetControlMaximum(theControl);
  187.     oldValue=GetControlValue(theControl);
  188.     numLines=TotalNumberOfLines(hTE);
  189.     max=numLines-(((**hTE).viewRect.bottom-(**hTE).viewRect.top)/((**hTE).lineHeight));
  190.     if (max<0)
  191.         max=0;
  192.     SetControlMaximum(theControl, max);
  193.     value=((**hTE).viewRect.top-(**hTE).destRect.top)/((**hTE).lineHeight);
  194.     if (value<0)
  195.         value=0;
  196.     else if (value>max)
  197.         value=max;
  198.     SetControlValue(theControl, value);
  199. }
  200.  
  201. short CurrentLineNumber(TEHandle hTE)
  202. {
  203.     short            i;
  204.     short            currentEndPoint;
  205.     short            lineNumber;
  206.     short            numLines;
  207.     
  208.     currentEndPoint=(**hTE).selEnd;
  209.     lineNumber=0;
  210.     numLines=(**hTE).nLines;
  211.     for (i=0; i<numLines; i++)
  212.     {
  213.         if ((**hTE).lineStarts[i]<currentEndPoint)
  214.             lineNumber++;
  215.         else
  216.             return lineNumber;
  217.     }
  218.     
  219.     return numLines;
  220. }
  221.  
  222. pascal Boolean MyClikLoop(void)
  223. {
  224.     Point            thePoint;
  225.     TEHandle        hTE;
  226.     WindowPtr        theWindow;
  227.     GrafPtr            curPort;
  228.     
  229.     theWindow=GetFrontDocumentWindow();
  230.     if (theWindow==0L)
  231.         theWindow=FrontWindow();
  232.     if (theWindow==0L)
  233.         return FALSE;
  234.     
  235.     hTE=GetWindowTE(theWindow);
  236.     if (hTE==0L)
  237.         return FALSE;
  238.     
  239.     GetPort(&curPort);
  240.     SetPort(theWindow);
  241.     GetMouse(&thePoint);
  242.     if (!PtInRect(thePoint, &((**hTE).viewRect)))
  243.     {
  244.         if (thePoint.v<((**hTE).viewRect.top))
  245.         {
  246.             if ((**hTE).selStart>0)
  247.                 TEPinScroll(0, (**hTE).lineHeight, hTE);
  248.         }
  249.         else if (thePoint.v>(**hTE).viewRect.bottom)
  250.         {
  251.             if ((**hTE).selEnd<(**hTE).teLength)
  252.                 TEPinScroll(0, -(**hTE).lineHeight, hTE);
  253.         }
  254.         
  255.         AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  256.     }
  257.     
  258.     SetPort(curPort);
  259.     
  260.     return TRUE;
  261. }
  262.  
  263. void AdjustForEndScroll(ControlHandle theControl, TEHandle hTE)
  264. {
  265.     short            numLines;
  266.     short            offset;
  267.     
  268.     numLines=TotalNumberOfLines(hTE);
  269.     offset=numLines-GetControlMaximum(theControl)-
  270.         (((**hTE).viewRect.bottom-(**hTE).viewRect.top)/(**hTE).lineHeight);
  271.     if (offset<0)
  272.         TEPinScroll(0, -offset*((**hTE).lineHeight), hTE);
  273. }
  274.  
  275. #define kTextMargin    2
  276.  
  277. /* Return a rectangle that is inset from the portRect by the size of
  278.     the scrollbars and a little extra margin. */
  279.  
  280. void GetTERect(WindowPtr window, Rect *teRect, Boolean adjustForScrollBars)
  281. {
  282.     *teRect = window->portRect;
  283.     InsetRect(teRect, kTextMargin, kTextMargin);    /* adjust for margin */
  284.     if (adjustForScrollBars)
  285.     {
  286.         teRect->bottom = teRect->bottom - 15;        /* and for the scrollbars */
  287.         teRect->right = teRect->right - 15;
  288.     }
  289. }
  290.  
  291.  
  292. /* Update the TERec's view rect so that it is the greatest multiple of
  293.     the lineHeight that still fits in the old viewRect. */
  294.  
  295. void AdjustViewRect(TEHandle docTE)
  296. {
  297.     TEPtr        te;
  298.     
  299.     te = *docTE;
  300.     te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / te->lineHeight)
  301.                             * te->lineHeight) + te->viewRect.top;
  302. }
  303.  
  304.  
  305. /*    Re-calculate the position and size of the viewRect and the scrollbars.
  306.     kScrollTweek compensates for off-by-one requirements of the scrollbars
  307.     to have borders coincide with the growbox. */
  308.  
  309. #define kScrollbarWidth        16
  310. #define kScrollbarAdjust    (kScrollbarWidth-1)
  311. #define kScrollTweek        2
  312.  
  313. void AdjustScrollSizes(WindowPtr window, TEHandle hTE, ControlHandle vScrollBar,
  314.     ControlHandle hScrollBar)
  315. {
  316.     Rect        teRect;
  317.     
  318.     GetTERect(window, &teRect, TRUE);                            /* start with TERect */
  319.     (**hTE).viewRect = teRect;
  320.     (**hTE).destRect.left=(**hTE).viewRect.left;
  321.     (**hTE).destRect.right=(**hTE).viewRect.right;
  322.     MoveControl(vScrollBar, window->portRect.right - kScrollbarAdjust, -1);
  323.     SizeControl(vScrollBar, kScrollbarWidth, (window->portRect.bottom - 
  324.                 window->portRect.top) - (kScrollbarAdjust - kScrollTweek));
  325.     MoveControl(hScrollBar, -1, window->portRect.bottom - kScrollbarAdjust);
  326.     SizeControl(hScrollBar, (window->portRect.right - 
  327.                 window->portRect.left) - (kScrollbarAdjust - kScrollTweek),
  328.                 kScrollbarWidth);
  329. }
  330.  
  331. void AdjustTE(TEHandle hTE, ControlHandle vScrollBar, ControlHandle hScrollBar)
  332. {
  333.     TEPtr        te;
  334.     
  335.     te = *hTE;
  336.     TEScroll((te->viewRect.left - te->destRect.left) -
  337.             GetControlValue(hScrollBar),
  338.             ((te->viewRect.top - te->destRect.top)/te->lineHeight) -
  339.                 GetControlValue(vScrollBar),
  340.             hTE);
  341. }
  342.  
  343. void DrawTheShadowBox(Rect theRect, Boolean eraseBackground)
  344. {
  345.     theRect.right-=2;
  346.     theRect.bottom-=2;
  347.     if (eraseBackground)
  348.         EraseRect(&theRect);
  349.     FrameRect(&theRect);
  350.     MoveTo(theRect.left+3, theRect.bottom+1);
  351.     Line(theRect.right-theRect.left-2, 0);
  352.     Line(0, -theRect.bottom+theRect.top+3);
  353.     MoveTo(theRect.left+3, theRect.bottom);
  354.     Line(theRect.right-theRect.left-3, 0);
  355.     Line(0, -theRect.bottom+theRect.top+4);
  356. }
  357.  
  358. void ZapDrawHook(TEHandle hTE)
  359. {
  360.     ProcPtr            temp;
  361.     
  362.     temp=(ProcPtr)MyDrawHook;
  363.     TECustomHook(intDrawHook, &temp, hTE);
  364.     gOldDrawHook=temp;
  365. }
  366.  
  367. void RestoreDrawHook(TEHandle hTE)
  368. {
  369.     ProcPtr            temp;
  370.     
  371.     temp=gOldDrawHook;
  372.     TECustomHook(intDrawHook, &temp, hTE);
  373. }
  374.  
  375. static    asm void MyDrawHook(void)
  376. {
  377.     rts
  378. }
  379.